| Total Complexity | 4 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { Extension } from 'nunjucks'; |
||
| 4 | |||
| 5 | // See: https://mozilla.github.io/nunjucks/api.html#custom-tags |
||
| 6 | export class TablesExtension implements Extension { |
||
| 7 | public readonly tags = ['table', 'inlinetable']; |
||
| 8 | |||
| 9 | constructor(private readonly templates: ITemplates) {} |
||
| 10 | |||
| 11 | public parse(parser: any, nodes: any, _lexer: any) { |
||
| 12 | const tagToken = parser.nextToken(); |
||
| 13 | |||
| 14 | const args = parser.parseSignature(null, true); |
||
| 15 | parser.advanceAfterBlockEnd(tagToken.value); |
||
| 16 | |||
| 17 | const methodName = |
||
| 18 | tagToken.value === 'table' ? 'renderTable' : 'renderInlineTable'; |
||
| 19 | |||
| 20 | return new nodes.CallExtension(this, methodName, args, []); |
||
| 21 | } |
||
| 22 | |||
| 23 | public renderTable( |
||
| 24 | _context: object, |
||
| 25 | table: Table, |
||
| 26 | extraContext: object = {} |
||
| 27 | ) { |
||
| 28 | const html = this.templates.render('tables/table.njk', { |
||
| 29 | table, |
||
| 30 | ...extraContext |
||
| 31 | }); |
||
| 32 | return this.templates.markSafe(html); |
||
| 33 | } |
||
| 34 | |||
| 35 | public renderInlineTable( |
||
| 36 | _context: object, |
||
| 37 | inline: Inline, |
||
| 38 | extraContext: object = {} |
||
| 39 | ) { |
||
| 40 | const html = this.templates.render('tables/inlinetable.njk', { |
||
| 41 | inline, |
||
| 42 | ...extraContext |
||
| 43 | }); |
||
| 44 | return this.templates.markSafe(html); |
||
| 45 | } |
||
| 47 |